package com.seed.core.configuration;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.seed.core.enumeration.ActionType;
import com.seed.core.pojo.seed.Action1;
import com.seed.core.pojo.seed.Stat;
import com.seed.util.StrKit;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ActionConfiguration extends HomeConfiguration {
    private ActionType type; // 行为类型
    private String declare; // 设置值的列名, 当类型为set时，这里表示要设置的申明
    private String declareValue; // 要设置的列的值
    private Map<String, ElementConfiguration> elements; // 各项申明对应的元素及元素属性，键为申明

    private String seedName; // 独立行为依赖的seed名称

    private String[] declares; // 行为所需要的数据列
    private String[] searches; // 如果是功能是page或list，这里申明对应的搜索拦，形如：name_like,age_eq等
    private String[] orders; // 排序
    private Map<String, StatConfiguration> stats; // 统计

    private Boolean checkLogin; // 是否需要检查登录
    private Boolean checkAction; // 是否需要检查功能权限

    public Action1 toAction() {
        Action1 ret = new Action1();
        ret.setType(type);

        ret.setDeclare(declare);
        ret.setDeclareValue(declareValue);

        ret.setSeedName(seedName);

        if(declares != null) ret.setDeclares(List.of(declares));
        if(searches != null) ret.setSearches(List.of(searches));
        if(orders != null) ret.setOrders(List.of(orders));

        if(stats != null) {
            Map<String, Stat> statMap = new HashMap<>();
            for(Map.Entry<String, StatConfiguration> entry: stats.entrySet()) {
                StatConfiguration statConfiguration = entry.getValue();
                Stat stat = new Stat();
                stat.setName(entry.getKey());
//                stat.setLabel(statConfiguration.getLabel());
//                stat.setComment(statConfiguration.getComment());
                stat.setType(statConfiguration.getType());
                stat.setDeclare(statConfiguration.getDeclare());
                stat.setSearches(statConfiguration.getSearches());
                statMap.put(entry.getKey(), stat);
            }
            ret.setStats(statMap);
        }

        ret.setCheckLogin(checkLogin);
        ret.setCheckAction(checkAction);
//        ret.setCheckData(checkData);
//        ret.setCheckField(checkField);

        return ret;
    }

    public ActionConfiguration toFront() {
        ActionConfiguration ret = new ActionConfiguration();
        ret.setType(type);
//        ret.setToolbar(toolbar);
//        ret.setRow(row);
        ret.setDeclare(declare);
        ret.setDeclareValue(declareValue);
        ret.setElements(elements);
        ret.setSeedName(seedName);

        ret.setDeclares(declares);
        if(searches != null) {
            List<String> frontSearches = new ArrayList<>();
            for(String search: searches) {
                String[] items = StrKit.cutInTwo(search, '=', false);
                if(StrUtil.isBlank(items[1])) frontSearches.add(search);
            }
            ret.setSearches(frontSearches.toArray(new String[0]));
        }
        if(orders != null) {
            List<String> frontOrders = new ArrayList<>();
            for(String order: orders) {
                String[] items = StrKit.cutInTwo(order, '=', false);
                if(StrUtil.isBlank(items[1])) frontOrders.add(order);
            }
            ret.setOrders(frontOrders.toArray(new String[0]));
        }

        ret.setStats(stats);

        ret.setCheckLogin(checkLogin);
        ret.setCheckAction(checkAction);
//        ret.setUseKeyword(useKeyword);
//        ret.setConfirm(confirm);

//        ret.setPosition(position);
        ret.setMode(mode);
        ret.setLabel(label);
        ret.setComment(comment);
        ret.setComponent(component);
        ret.setLayout(layout);
        ret.setComponentProps(componentProps);
        ret.setPopupProps(popupProps);

        ret.setIcon(icon);
        ret.setColor(color);
        ret.setSize(size);
        ret.setHoverColor(hoverColor);

        ret.setAffix(affix);
        ret.setHidden(hidden);
        ret.setBreadcrumb(breadcrumb);
        ret.setNoTagsView(noTagsView);

        return ret;
    }

    public ActionType getType() {
        return type;
    }

    public void setType(ActionType type) {
        this.type = type;
    }

//    public String getToolbar() {
//        return toolbar;
//    }
//
//    public void setToolbar(String toolbar) {
//        this.toolbar = toolbar;
//    }
//
//    public String getRow() {
//        return row;
//    }
//
//    public void setRow(String row) {
//        this.row = row;
//    }

    public String getDeclare() {
        return declare;
    }

    public void setDeclare(String declare) {
        this.declare = declare;
    }

    public String getDeclareValue() {
        return declareValue;
    }

    public void setDeclareValue(String declareValue) {
        this.declareValue = declareValue;
    }

    public Map<String, ElementConfiguration> getElements() {
        return elements;
    }

    public void setElements(Map<String, ElementConfiguration> elements) {
        this.elements = elements;
    }

    public String getSeedName() {
        return seedName;
    }

    public void setSeedName(String seedName) {
        this.seedName = seedName;
    }

    public String[] getDeclares() {
        return declares;
    }

    public void setDeclares(String[] declares) {
        this.declares = declares;
    }

    public String[] getSearches() {
        return searches;
    }

    public void setSearches(String[] searches) {
        this.searches = searches;
    }

    public String[] getOrders() {
        return orders;
    }

    public void setOrders(String[] orders) {
        this.orders = orders;
    }

    public Map<String, StatConfiguration> getStats() {
        return stats;
    }

    public void setStats(Map<String, StatConfiguration> stats) {
        this.stats = stats;
    }

    public Boolean getCheckLogin() {
        return checkLogin;
    }

    public void setCheckLogin(Boolean checkLogin) {
        this.checkLogin = checkLogin;
    }

    public Boolean getCheckAction() {
        return checkAction;
    }

    public void setCheckAction(Boolean checkAction) {
        this.checkAction = checkAction;
    }

//    public Boolean getCheckData() {
//        return checkData;
//    }
//
//    public void setCheckData(Boolean checkData) {
//        this.checkData = checkData;
//    }
//
//    public Boolean getCheckField() {
//        return checkField;
//    }
//
//    public void setCheckField(Boolean checkField) {
//        this.checkField = checkField;
//    }
//
//    public Boolean getUseKeyword() {
//        return useKeyword;
//    }
//
//    public void setUseKeyword(Boolean useKeyword) {
//        this.useKeyword = useKeyword;
//    }

//    public String getConfirm() {
//        return confirm;
//    }
//
//    public void setConfirm(String confirm) {
//        this.confirm = confirm;
//    }
}
